package com.jd.risk.bankorder.util;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class HttpRequestUtils {
	private static Logger LOGGER = LoggerFactory.getLogger(HttpRequestUtils.class);
	
	public static String get(String httpUri) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		String result = null;
		try {
			HttpGet get = new HttpGet(httpUri);
			CloseableHttpResponse httpResponse = null;
			httpResponse = httpClient.execute(get);
			try {
				// response实体
				HttpEntity entity = httpResponse.getEntity();
				if(httpResponse.getStatusLine().getStatusCode() == 200){
					result = EntityUtils.toString(entity);
				}
			} finally {
				httpResponse.close();
			}
		} catch (Exception e) {
			LOGGER.error("",e);
			e.printStackTrace();
		} finally {
			try {
				if (httpClient != null) {
					httpClient.close();
				}
			} catch (IOException e) {
				LOGGER.error("",e);
			}
		}
		return result;
	}
}
